/**
 * 基础js，封装常用工具
 * 基于JQ
 */

var artIcon = {
    "tips": "//f1.yihuimg.com/concat/cross_pc/1.1.1/widget/artDialog/4.1.7/images/tips-icon.png",
    "loading": "//f1.yihuimg.com/concat/cross_pc/1.1.1/widget/artDialog/4.1.7/images/loading.gif"
};

$.extend({
    "art": function (param) {
        var artIndex = false;
        try {
            artIndex = art.dialog(param);
        } catch (e) {
            alert("弹窗组件未能正常导入");
        }
        return artIndex;
    },
    "alert": function (msg, param) {
        var defParam = {
            id: "alert",
            lock: true,
            artIcon: 'ask',
            opacity: 0.4,
            width: 250,
            title: '提示',
            ok: emptyFunc
        };

        if (typeof param === 'object') {
            defParam = $.extend(defParam, param);
        }

        defParam.content = msg ? msg : '';
        return $.art(defParam);
    },
    "toast": function (msg, time, callback) {
        time = time ? time : 3000;
        if (typeof callback === 'function') {
            setTimeout(function () {
                callback();
            }, time);
        }

        return $.art({
            id: "toast",
            lock: true,
            content: '<img src="' + artIcon.tips + '" class="mr10" />' + msg,
            tips: true,
            time: time / 1000
        });
    },
    "loading": function (msg) {
        msg = msg ? msg : "加载中";
        return $.art({
            id: "loading",
            lock: true,
            content: '<img src="' + artIcon.loading + '" class="mr10" />' + msg,
            tips: true
        })
    },
    "sAjax": function (param) {
        var def = {
            "timeout": 80000,
            "type": "post",
            "async": true,
            "loading": true,
            "title": void(0)
        };
        var param = $.extend(def, param);

        if (typeof param.data === 'object' && typeof $.urlParam === 'object') {
            var urlParam = $.urlParam;
            param.data = $.extend(param.data, urlParam);
        }

        if (param.loading) {
            var loadingIndex = $.loading();
            var defComplete = param.complete;
            param.complete = function () {
                try {
                    loadingIndex.close();
                } catch (e) {
                }
                if (typeof defComplete === "function") {
                    defComplete();
                }
            };
        }

        var error = param.error;
        if (typeof error !== "function") {
            param.error = function () {
                $.alert("网络响应超时");
                return false;
            };
        }

        return $.ajax(param);
    },
    /**
     * 为url加装用户信息
     * @param {type} url
     * @param {type} param
     * @returns {unresolved}
     */
    "sUrl": function (url, param) {
        if (url.indexOf("http://") === -1 && url.indexOf("https://") === -1) {
            if ($.domain) {
                url = $.domain + url;
            }
        }

        var defParam = $.urlParam;
        if (!defParam) {
            defParam = {};
        }

        if (typeof param === 'object') {
            defParam = $.extend(defParam, param);
        }

        if (url.indexOf("?") === -1) {
            url += "?";
        } else {
            url += "&";
        }

        url += $.param(defParam);
        return url;
    }
});


/**
 * 常用打印
 * @param msg
 * @constructor
 */
function D_B(msg) {
    try {
        console.log(msg);
    } catch (e) {
    }
}
;

/**
 * 返回值的封装
 * @param code
 * @param msg
 * @param result
 * @returns {{Code: *, Message: *}}
 */
function baseRes(code, msg, result) {
    var resObj = {
        "Code": code,
        "Message": msg
    };

    if (typeof result != "undefined") {
        resObj.Result = result;
    }
    return resObj
}

/**
 * 标准的成功返回
 * @param msg
 * @param result
 * @param code
 * @returns {{Code: *, Message: *}}
 */
function sucRes(msg, result, code) {
    if (typeof code == "undefined") {
        code = 10000;
    }
    return baseRes(code, msg, result);
}

/**
 * 标准的错误返回
 * @param msg
 * @param code
 * @returns {{Code: *, Message: *}}
 */
function errRes(msg, code) {
    if (typeof code == "undefined") {
        code = -10000;
    }
    return baseRes(code, msg);
}

/**
 * 空方法
 */
function emptyFunc() {
    D_B("空方法");
}

/**
 * js自动跳转
 * 给url包装sUrl和自动埋点
 * @param {type} param1
 * @param {type} param2
 * @param {type} param3
 */
$(document).on('click', '.jump-href', function () {
    var href = $(this).attr('data-href');

    if (!href) {
        location.reload();
    } else {
        location.href = $.sUrl(href);
    }
});
